/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cx1.dao.api.IPlaceDao;
import org.cx1.model.Place;

/**
 *
 * @author tina
 */
public class PlaceDao implements IPlaceDao {

    private SqlMapClient sqlMapClient;

    public PlaceDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
   
    @Override
    public List<Place> getAll() {
        List<Place> places = new ArrayList<Place>();
        
        try {
            places = sqlMapClient.queryForList("place.getAll");
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return places;
    }
        
    @Override
    public Place getById(String id) {
        Place place = null;
        
        try {
            place = (Place) sqlMapClient.queryForObject("place.getById", id);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return place;
    }
    
    @Override
    public String add(Place place) {
        String placeId = "";
        
        try {
            sqlMapClient.insert("place.add", place);
            placeId = place.getId();
            
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return placeId;
    }
    
    @Override
    public int update(Place place) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.update("place.update", place);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }
    
    @Override
    public int deleteById(String id) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.delete("place.deleteById", id);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }
        
    @Override
    public boolean addPhoto(String placeId, String photoId) {
        boolean isSuccessful = false;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeId", placeId);
        map.put("photoId", photoId);

        try {
            sqlMapClient.insert("place.addPhoto", map);
            isSuccessful = true;
            
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return isSuccessful;
    }
    
    @Override
    public int deletePhoto(String placeId, String photoId) {
        int noOfRowsAffected = 0;
        
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeId", placeId);
        map.put("photoId", photoId);

        try {
            noOfRowsAffected = sqlMapClient.delete("place.deletePhoto", map);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public List<String> getPhotoIds(String placeId) {
        List<String> photoIds = new ArrayList<String>();
        try {
            photoIds = (ArrayList<String>)sqlMapClient.queryForList("place.getPhotoIds", placeId);
        } catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return photoIds;
    }

    @Override
    public List<String> getAllPlaceRefIDs(String placeSrcID, String placeDestTypeID) {
        List<String> placeRefIDs = new ArrayList<String>();
        try {
            Map<String,String> map = new HashMap<String, String>();
            map.put("placeSrcID", placeSrcID);
            map.put("placeDestTypeID", placeDestTypeID);
            placeRefIDs = (ArrayList<String>)sqlMapClient.queryForList("place.getAllPlaceRefIDs", map);            
        } 
        catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return placeRefIDs;
    }

    @Override
    public String addPlaceRef(String placeSrcID, String placeDestID, String placeDestTypeID) {
        String placeId = "";        
        try {
            Map<String,String> map = new HashMap<String, String>();
            map.put("placeSrcID", placeSrcID);
            map.put("placeDestID", placeDestID);
            map.put("placeDestTypeID", placeDestTypeID);
            
            sqlMapClient.insert("place.addPlaceRef", map);   
        } 
        catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return placeId;
    }

    @Override
    public int deleteAllPlaceRef(String placeSrcID) {
        int noOfRowsAffected = 0;        
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeSrcID", placeSrcID);
        
        try {
            noOfRowsAffected = sqlMapClient.delete("place.deleteAllPlaceRef", map);
        } 
        catch (SQLException ex) {
            Logger.getLogger(PlaceDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return noOfRowsAffected;
    }

    @Override
    public int incrementImpressions(String placeID) {   
        int noOfRowsAffected = 0;     
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeID", placeID);        
        
        try {
            noOfRowsAffected = sqlMapClient.update("place.incrementImpressions", map);
        } 
        catch (SQLException ex) {
            Logger.getLogger(ListingDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return noOfRowsAffected;
    }

    @Override
    public int incrementViews(String placeID) {        
        int noOfRowsAffected = 0;     
        Map<String,String> map = new HashMap<String, String>();
        map.put("placeID", placeID);        
        
        try {
            noOfRowsAffected = sqlMapClient.update("place.incrementViews", map);
        } 
        catch (SQLException ex) {
            Logger.getLogger(ListingDao.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return noOfRowsAffected;
    }
    
}
